package david.java.tools;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: groupingset 生成工具   注意: 必须与级联不能有交集
 *
 * @Author: ZhaoDawei
 * @Date: Create in 7:22 下午 2022/10/19
 */
public class GroupingSetUtil extends BaseUtil {
    static String output = "target-tmp/tmp_groupingset";

    /**
     * 全部维度: 所有维度需要写在这里
     */
    static String ALL_DIM = "";

    /**
     * 强制维度: 必须存在
     */
    static String FORCE_DIM = "";

    /**
     * 级联维度: 多个组合用 ';'分隔
     */
    static String GRADE_DIM = "";

    /**
     * 禁止维度: 任意两个不能同时出现,多个组合用 ';'分隔
     */
    static String FORBID_DIM = "";


    public static void main(String[] args) {
        checkDuplicate(ALL_DIM);
        mkdirIfNotExists(output);

        List<String> all = Arrays.stream(ALL_DIM.split(",")).map(String::trim).collect(Collectors.toList());
        List<String> force = Arrays.stream(FORCE_DIM.split(",")).map(String::trim).collect(Collectors.toList());

        List<String> grade = new ArrayList<>();

        // Map<String, List<String>> grade = new HashMap<>();
        Map<String, Set<String>> forbid = new HashMap<>();


        HashSet<String> forceSet = new HashSet<>(force);
        List<List<String>> result = new ArrayList<>();
        Map<String, Integer> duplicate = new HashMap<>();

        Set<String> reject = new HashSet<>(grade);


        String[] gradeSplits = GRADE_DIM.split(";");
        for (String str : gradeSplits) {
            if (StringUtils.isNotBlank(str)) {
                String[] split = str.split(",");
                List<List<String>> l1 = new ArrayList<>();

                for (int i = 0; i < split.length; i++) {
                    reject.add(split[0]);
                    if (i == 0) {
                        ArrayList<String> l2 = new ArrayList<>();
                        l2.add(split[0]);
                        l1.add(l2);
                    } else {


                    }
                }
            }
        }

        String[] forbidSplits = FORBID_DIM.split(";");
        for (String str : forbidSplits) {
            if (StringUtils.isNotBlank(str)) {
                String[] split = str.split(",");
                if (split.length > 1) {
                    for (int i = split.length - 1; i >= 1; i--) {
                        String key = split[i];
                        Set<String> set = new HashSet<>();
                        for (int j = 0; j < i - 1; j++) {
                            set.add(split[j].trim());
                        }
                        forbid.put(key, set);
                    }
                }
            }
        }

        boolean hasForce = force.size() > 0;
        // boolean hasGrade = grade.keySet().size() > 0;
        boolean hasForbid = forbid.keySet().size() > 0;

        for (int i = 0; i < all.size() ; i++) {
            ArrayList<String> list = new ArrayList<>();
            list.addAll(force);
            if (!forceSet.contains(all.get(i))) {
                for (int j = i; j < all.size(); j++) {

                }

            }
            String col = all.get(i);

        }
    }


    private static int hashcode(List<String> list) {
        ArrayList<String> dest = new ArrayList<>();
        Collections.copy(dest, list);
        Collections.sort(dest);
        return String.join(",", dest).hashCode();
    }

    private static void checkDuplicate(String str) {
        String[] split = str.split(",");
        HashMap<String, Integer> map = new HashMap<>();
        for (String s: split) {
            map.put(s, map.getOrDefault(s, 0) + 1);
        }
        ArrayList<String> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry: map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            if (value > 1) {
                list.add(key + " appear " + value + " times");
            }
        }
        throw new RuntimeException(String.join("\n", list));

    }
}
